package com.eric.service.impl;

import cn.dev33.satoken.util.SaResult;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.eric.domain.dto.SupplierDTO;
import com.eric.domain.po.Supplier;
import com.eric.domain.vo.SupplierVO;
import com.eric.mapper.SupplierMapper;
import com.eric.service.SupplierService;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.update.UpdateChain;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;

import static com.eric.domain.po.table.SupplierTableDef.SUPPLIER;

/**
 *  服务层实现。
 *
 * @author eric.deng
 * @since 2025-03-05
 */
@Service
public class SupplierServiceImpl extends ServiceImpl<SupplierMapper, Supplier>  implements SupplierService{

    @Override
    public SaResult saveOne(SupplierVO supplierVO) {
        Supplier one = queryChain().select()
                .where(SUPPLIER.SUPPLIER_NAME.eq(supplierVO.getSupplierName()))
                .one();
        boolean empty = BeanUtil.isEmpty(one);
        if (empty){
            Supplier supplier=new Supplier();
            BeanUtil.copyProperties(supplierVO,supplier);
            boolean save = save(supplier);
            return save ? SaResult.ok("已录入！") : (SaResult.error("录入错误"));
        }else {
            return SaResult.error("供应商已经存在！");
        }



    }

    @Override
    public  SaResult getListByPage(Integer cur, Integer pageSize) {
        if (cur==null){
            cur=1;
        }
        if (pageSize==null){
            pageSize=11;
        }

        List<Supplier> supplierList = queryChain().select()
                .from(SUPPLIER).orderBy(SUPPLIER.UPDATE_TIME.desc())
                .page(new Page<>(cur, pageSize))
                .getRecords();
        if (!supplierList.isEmpty()){
            List<SupplierDTO> supplierDTOList = BeanUtil.copyToList(supplierList, SupplierDTO.class);
            return SaResult.data(supplierDTOList);
        }


        return SaResult.error("没有数据！");
    }

    @Override
    @Transactional
    public SaResult updateByIdService(SupplierDTO supplierDTO) {
        System.out.println(supplierDTO);
        boolean supplierNameEffective=false;
        boolean supplierPhoneEffective=false;
        boolean supplierContactEffective=false;
        boolean supplierDescriptionEffective=false;
        boolean supplierAddressEffective=false;
        boolean supplierEmailEffective=false;
        boolean supplierInvoiceInfoEffective=false;

        if (supplierDTO.getId()==null){
            return SaResult.data("数据错误！");
        }
        if (!StrUtil.isBlank(supplierDTO.getSupplierName())){
            supplierNameEffective=true;
        }
        if (!StrUtil.isBlank(supplierDTO.getSupplierContact())){
            supplierContactEffective=true;
        }
        if (!StrUtil.isBlank(supplierDTO.getSupplierPhone())){
            supplierPhoneEffective=true;
        }
        if (!StrUtil.isBlank(supplierDTO.getDescription())){
            supplierDescriptionEffective=true;
        }
        if (!StrUtil.isBlank(supplierDTO.getAddress())){
            supplierAddressEffective=true;
        }
        if (!StrUtil.isBlank(supplierDTO.getEmail())){
            supplierEmailEffective=true;
        }
        if (!StrUtil.isBlank(supplierDTO.getInvoiceInfo())){
            supplierInvoiceInfoEffective=true;
        }


        Supplier one = queryChain().where(SUPPLIER.SUPPLIER_NAME.eq(supplierDTO.getSupplierName())).one();
//        如果通过supplierName查到的one不为空，并且id和supplierDTO中的id不相等，就说明供应商名称重复
        if (!BeanUtil.isEmpty(one) &&!Objects.equals(one.getId(),supplierDTO.getId())){
            System.out.println("供应商名称已存在！");
            return SaResult.error("供应商名称已存在！");
        }

        boolean updated = UpdateChain.of(Supplier.class)
                .set(SUPPLIER.SUPPLIER_NAME, supplierDTO.getSupplierName(),supplierNameEffective)
                .set(SUPPLIER.SUPPLIER_CONTACT,supplierDTO.getSupplierContact() ,supplierContactEffective)
                .set(SUPPLIER.SUPPLIER_PHONE,supplierDTO.getSupplierPhone() ,supplierPhoneEffective)
                .set(SUPPLIER.DESCRIPTION, supplierDTO.getDescription(),supplierDescriptionEffective)
                .set(SUPPLIER.ADDRESS, supplierDTO.getAddress(),supplierAddressEffective)
                .set(SUPPLIER.EMAIL, supplierDTO.getEmail(),supplierEmailEffective)
                .set(SUPPLIER.INVOICE_INFO, supplierDTO.getInvoiceInfo(),supplierInvoiceInfoEffective)
                .where(SUPPLIER.ID.eq(supplierDTO.getId()))
                .update();
        return SaResult.data(updated);
    }

    @Override
    public SaResult getSupplierListByNameService(String name,Integer cur,Integer pageSize) {
        if (cur==null||cur<=0){
            cur=1;
        }
        if (pageSize==null||pageSize<=0){
            pageSize=11;
        }

        List<Supplier> listBySupplierName = queryChain().select()
                .where(SUPPLIER.SUPPLIER_NAME.like(name, true))
                .orderBy(SUPPLIER.UPDATE_TIME.desc())
                .page(new Page<>(cur, pageSize)).getRecords();
        List<Supplier> ListByContact = queryChain().select().where(SUPPLIER.SUPPLIER_CONTACT.like(name, true))
                .orderBy(SUPPLIER.UPDATE_TIME.desc())
                .page(new Page<>(cur, pageSize)).getRecords();
        List<Supplier> listByPhone = queryChain().select().where(SUPPLIER.SUPPLIER_PHONE.like(name, true))
                .orderBy(SUPPLIER.UPDATE_TIME.desc())
                .page(new Page<>(cur, pageSize)).getRecords();
        Set<Supplier> supplierSet = new HashSet<>() ;
        supplierSet.addAll(listBySupplierName);
        supplierSet.addAll(ListByContact);
        supplierSet.addAll(listByPhone);

        List<SupplierDTO> supplierDTOList = BeanUtil.copyToList(supplierSet, SupplierDTO.class);

        return SaResult.data(supplierDTOList);
    }

    @Override
    public SaResult getSupplierListByNameCountService(String name) {

        List<Supplier> listByName = queryChain().select().where(SUPPLIER.SUPPLIER_NAME.like(name, true)).list();
        List<Supplier> listByContact = queryChain().select().where(SUPPLIER.SUPPLIER_CONTACT.like(name, true)).list();
        List<Supplier> listByPhone = queryChain().select().where(SUPPLIER.SUPPLIER_PHONE.like(name, true)).list();
        Set<Supplier>supplierSet=new HashSet<>();
        supplierSet.addAll(listByName);
        supplierSet.addAll(listByContact);
        supplierSet.addAll(listByPhone);
        return SaResult.data(supplierSet.size());
    }
}


